﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using PlasmaMes.A_Models;
using PlasmaMes.Services;

namespace PlasmaMes
{
    public partial class Form_Rfid_Connect: Form
    {
        public Form_Rfid_Connect()
        {
            InitializeComponent();
        }

        private void uiButton_连接_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(tb_ip.Text))
            {
                MessageBox.Show("IP地址不能为空");
                return;
            }

            if(string.IsNullOrEmpty(tb_port.Text))
            {
                MessageBox.Show("端口不能为空");
                return;
            }

            if(Rfid.Instance.EnServiceConnect(tb_ip.Text, tb_port.Text))
            {
                LogService.Instance.LogInfo("RFID连接成功");
            }
            else
            {
                LogService.Instance.LogError("RFID连接失败");
            }

        }

        private void uiButton_断开_Click(object sender, EventArgs e)
        {
            Rfid.Instance.EnServiceDisConnect();
            LogService.Instance.LogInfo("断开成功");
        }

        private void uiCheckBox_GPO1_CheckedChanged(object sender, EventArgs e)
        {
            if(Rfid.Instance.isConnect == false)
            {
                MessageBox.Show("请先连接RFID");
                return;
            }

            if(uiCheckBox_GPO1.Checked)
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_1] = true }))
                {
                    LogService.Instance.LogInfo("启用GPO1输出 成功");
                    RunParam.Instance.IsUseGPO1 = true;
                }
                else
                {
                    LogService.Instance.LogError("启用GPO1输出 失败");
                }
            }
            else
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_1] = false }))
                {
                    LogService.Instance.LogInfo("关闭启用GPO1输出 成功");
                    RunParam.Instance.IsUseGPO1 = false;
                }
                else
                {
                    LogService.Instance.LogError("关闭启用GPO1输出 失败");
                }
            } 
        }

        private void uiCheckBox_GPO2_CheckedChanged(object sender, EventArgs e)
        {
            if (Rfid.Instance.isConnect == false)
            {
                MessageBox.Show("请先连接RFID");
                return;
            }

            if (uiCheckBox_GPO2.Checked)
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_2] = true }))
                {
                    LogService.Instance.LogInfo("启用GPO2输出 成功");
                }
                else
                {
                    LogService.Instance.LogError("启用GPO2输出 失败");
                }
            }
            else
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_2] = false }))
                {
                    LogService.Instance.LogInfo("关闭启用GPO2输出 成功");
                }
                else
                {
                    LogService.Instance.LogError("关闭启用GPO2输出 失败");
                }
            }
        }

        private void uiCheckBox_GPO3_CheckedChanged(object sender, EventArgs e)
        {
            if (Rfid.Instance.isConnect == false)
            {
                MessageBox.Show("请先连接RFID");
                return;
            }

            if (uiCheckBox_GPO3.Checked)
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_3] = true }))
                {
                    LogService.Instance.LogInfo("启用GPO3输出 成功");
                }
                else
                {
                    LogService.Instance.LogError("启用GPO3输出 失败");
                }
            }
            else
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_3] = false }))
                {
                    LogService.Instance.LogInfo("关闭启用GPO3输出 成功");
                }
                else
                {
                    LogService.Instance.LogError("关闭启用GPO3输出 失败");
                }
            }
        }

        private void uiCheckBox_GPO4_CheckedChanged(object sender, EventArgs e)
        {
            if (Rfid.Instance.isConnect == false)
            {
                MessageBox.Show("请先连接RFID");
                return;
            }

            if (uiCheckBox_GPO4.Checked)
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_4] = true }))
                {
                    LogService.Instance.LogInfo("启用GPO4输出 成功");
                }
                else
                {
                    LogService.Instance.LogError("启用GPO4输出 失败");
                }
            }
            else
            {
                if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_4] = false }))
                {
                    LogService.Instance.LogInfo("关闭启用GPO4输出 成功");
                }
                else
                {
                    LogService.Instance.LogError("关闭启用GPO4输出 失败");
                }
            }
        }

        private void bt_保存参数_Click(object sender, EventArgs e)
        {
            RunParam.Instance.Rfid_Addr = tb_Rf1_地址.Text;
            RunParam.Instance.Rfid_Length = tb_Rf1_长度.Text;
        }
    }
}
